package qldc.pm.tienan.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException;
import qldc.pm.tienan.model.TEMP_DoiTuongTienAn;
import qldc.pm.tienan.model.impl.TEMP_DoiTuongTienAnImpl;
import qldc.pm.tienan.model.impl.TEMP_DoiTuongTienAnModelImpl;
import qldc.pm.tienan.service.persistence.TEMP_DoiTuongTienAnPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the t e m p_ doi tuong tien an service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see TEMP_DoiTuongTienAnPersistence
 * @see TEMP_DoiTuongTienAnUtil
 * @generated
 */
public class TEMP_DoiTuongTienAnPersistenceImpl extends BasePersistenceImpl<TEMP_DoiTuongTienAn>
    implements TEMP_DoiTuongTienAnPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link TEMP_DoiTuongTienAnUtil} to access the t e m p_ doi tuong tien an persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = TEMP_DoiTuongTienAnImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_FETCH_BY_SODINHDANHCANHAN = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class, FINDER_CLASS_NAME_ENTITY,
            "fetchBySoDinhDanhCaNhan", new String[] { String.class.getName() },
            TEMP_DoiTuongTienAnModelImpl.SODINHDANHCANHAN_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_SODINHDANHCANHAN = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countBySoDinhDanhCaNhan", new String[] { String.class.getName() });
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1 =
        "temp_DoiTuongTienAn.soDinhDanhCaNhan IS NULL";
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2 =
        "temp_DoiTuongTienAn.soDinhDanhCaNhan = ?";
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3 =
        "(temp_DoiTuongTienAn.soDinhDanhCaNhan IS NULL OR temp_DoiTuongTienAn.soDinhDanhCaNhan = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUserId",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID =
        new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUserId",
            new String[] { Long.class.getName() },
            TEMP_DoiTuongTienAnModelImpl.USERID_COLUMN_BITMASK |
            TEMP_DoiTuongTienAnModelImpl.CREATEDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
            new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_USERID_USERID_2 = "temp_DoiTuongTienAn.userId = ?";
    private static final String _SQL_SELECT_TEMP_DOITUONGTIENAN = "SELECT temp_DoiTuongTienAn FROM TEMP_DoiTuongTienAn temp_DoiTuongTienAn";
    private static final String _SQL_SELECT_TEMP_DOITUONGTIENAN_WHERE = "SELECT temp_DoiTuongTienAn FROM TEMP_DoiTuongTienAn temp_DoiTuongTienAn WHERE ";
    private static final String _SQL_COUNT_TEMP_DOITUONGTIENAN = "SELECT COUNT(temp_DoiTuongTienAn) FROM TEMP_DoiTuongTienAn temp_DoiTuongTienAn";
    private static final String _SQL_COUNT_TEMP_DOITUONGTIENAN_WHERE = "SELECT COUNT(temp_DoiTuongTienAn) FROM TEMP_DoiTuongTienAn temp_DoiTuongTienAn WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "temp_DoiTuongTienAn.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No TEMP_DoiTuongTienAn exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No TEMP_DoiTuongTienAn exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(TEMP_DoiTuongTienAnPersistenceImpl.class);
    private static TEMP_DoiTuongTienAn _nullTEMP_DoiTuongTienAn = new TEMP_DoiTuongTienAnImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<TEMP_DoiTuongTienAn> toCacheModel() {
                return _nullTEMP_DoiTuongTienAnCacheModel;
            }
        };

    private static CacheModel<TEMP_DoiTuongTienAn> _nullTEMP_DoiTuongTienAnCacheModel =
        new CacheModel<TEMP_DoiTuongTienAn>() {
            @Override
            public TEMP_DoiTuongTienAn toEntityModel() {
                return _nullTEMP_DoiTuongTienAn;
            }
        };

    public TEMP_DoiTuongTienAnPersistenceImpl() {
        setModelClass(TEMP_DoiTuongTienAn.class);
    }

    /**
     * Returns the t e m p_ doi tuong tien an where soDinhDanhCaNhan = &#63; or throws a {@link qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException} if it could not be found.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the matching t e m p_ doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn findBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = fetchBySoDinhDanhCaNhan(soDinhDanhCaNhan);

        if (temp_DoiTuongTienAn == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("soDinhDanhCaNhan=");
            msg.append(soDinhDanhCaNhan);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isWarnEnabled()) {
                _log.warn(msg.toString());
            }

            throw new NoSuchTEMP_DoiTuongTienAnException(msg.toString());
        }

        return temp_DoiTuongTienAn;
    }

    /**
     * Returns the t e m p_ doi tuong tien an where soDinhDanhCaNhan = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the matching t e m p_ doi tuong tien an, or <code>null</code> if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn fetchBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws SystemException {
        return fetchBySoDinhDanhCaNhan(soDinhDanhCaNhan, true);
    }

    /**
     * Returns the t e m p_ doi tuong tien an where soDinhDanhCaNhan = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @param retrieveFromCache whether to use the finder cache
     * @return the matching t e m p_ doi tuong tien an, or <code>null</code> if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn fetchBySoDinhDanhCaNhan(
        String soDinhDanhCaNhan, boolean retrieveFromCache)
        throws SystemException {
        Object[] finderArgs = new Object[] { soDinhDanhCaNhan };

        Object result = null;

        if (retrieveFromCache) {
            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    finderArgs, this);
        }

        if (result instanceof TEMP_DoiTuongTienAn) {
            TEMP_DoiTuongTienAn temp_DoiTuongTienAn = (TEMP_DoiTuongTienAn) result;

            if (!Validator.equals(soDinhDanhCaNhan,
                        temp_DoiTuongTienAn.getSoDinhDanhCaNhan())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_TEMP_DOITUONGTIENAN_WHERE);

            boolean bindSoDinhDanhCaNhan = false;

            if (soDinhDanhCaNhan == null) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1);
            } else if (soDinhDanhCaNhan.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3);
            } else {
                bindSoDinhDanhCaNhan = true;

                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindSoDinhDanhCaNhan) {
                    qPos.add(soDinhDanhCaNhan);
                }

                List<TEMP_DoiTuongTienAn> list = q.list();

                if (list.isEmpty()) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                        finderArgs, list);
                } else {
                    if ((list.size() > 1) && _log.isWarnEnabled()) {
                        _log.warn(
                            "TEMP_DoiTuongTienAnPersistenceImpl.fetchBySoDinhDanhCaNhan(String, boolean) with parameters (" +
                            StringUtil.merge(finderArgs) +
                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
                    }

                    TEMP_DoiTuongTienAn temp_DoiTuongTienAn = list.get(0);

                    result = temp_DoiTuongTienAn;

                    cacheResult(temp_DoiTuongTienAn);

                    if ((temp_DoiTuongTienAn.getSoDinhDanhCaNhan() == null) ||
                            !temp_DoiTuongTienAn.getSoDinhDanhCaNhan()
                                                    .equals(soDinhDanhCaNhan)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                            finderArgs, temp_DoiTuongTienAn);
                    }
                }
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (TEMP_DoiTuongTienAn) result;
        }
    }

    /**
     * Removes the t e m p_ doi tuong tien an where soDinhDanhCaNhan = &#63; from the database.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the t e m p_ doi tuong tien an that was removed
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn removeBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

        return remove(temp_DoiTuongTienAn);
    }

    /**
     * Returns the number of t e m p_ doi tuong tien ans where soDinhDanhCaNhan = &#63;.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the number of matching t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_SODINHDANHCANHAN;

        Object[] finderArgs = new Object[] { soDinhDanhCaNhan };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_TEMP_DOITUONGTIENAN_WHERE);

            boolean bindSoDinhDanhCaNhan = false;

            if (soDinhDanhCaNhan == null) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1);
            } else if (soDinhDanhCaNhan.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3);
            } else {
                bindSoDinhDanhCaNhan = true;

                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindSoDinhDanhCaNhan) {
                    qPos.add(soDinhDanhCaNhan);
                }

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Returns all the t e m p_ doi tuong tien ans where userId = &#63;.
     *
     * @param userId the user ID
     * @return the matching t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_DoiTuongTienAn> findByUserId(long userId)
        throws SystemException {
        return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the t e m p_ doi tuong tien ans where userId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_DoiTuongTienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param userId the user ID
     * @param start the lower bound of the range of t e m p_ doi tuong tien ans
     * @param end the upper bound of the range of t e m p_ doi tuong tien ans (not inclusive)
     * @return the range of matching t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_DoiTuongTienAn> findByUserId(long userId, int start,
        int end) throws SystemException {
        return findByUserId(userId, start, end, null);
    }

    /**
     * Returns an ordered range of all the t e m p_ doi tuong tien ans where userId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_DoiTuongTienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param userId the user ID
     * @param start the lower bound of the range of t e m p_ doi tuong tien ans
     * @param end the upper bound of the range of t e m p_ doi tuong tien ans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_DoiTuongTienAn> findByUserId(long userId, int start,
        int end, OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID;
            finderArgs = new Object[] { userId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
            finderArgs = new Object[] { userId, start, end, orderByComparator };
        }

        List<TEMP_DoiTuongTienAn> list = (List<TEMP_DoiTuongTienAn>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (TEMP_DoiTuongTienAn temp_DoiTuongTienAn : list) {
                if ((userId != temp_DoiTuongTienAn.getUserId())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_TEMP_DOITUONGTIENAN_WHERE);

            query.append(_FINDER_COLUMN_USERID_USERID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(TEMP_DoiTuongTienAnModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(userId);

                if (!pagination) {
                    list = (List<TEMP_DoiTuongTienAn>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TEMP_DoiTuongTienAn>(list);
                } else {
                    list = (List<TEMP_DoiTuongTienAn>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first t e m p_ doi tuong tien an in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching t e m p_ doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn findByUserId_First(long userId,
        OrderByComparator orderByComparator)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = fetchByUserId_First(userId,
                orderByComparator);

        if (temp_DoiTuongTienAn != null) {
            return temp_DoiTuongTienAn;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("userId=");
        msg.append(userId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTEMP_DoiTuongTienAnException(msg.toString());
    }

    /**
     * Returns the first t e m p_ doi tuong tien an in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching t e m p_ doi tuong tien an, or <code>null</code> if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn fetchByUserId_First(long userId,
        OrderByComparator orderByComparator) throws SystemException {
        List<TEMP_DoiTuongTienAn> list = findByUserId(userId, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last t e m p_ doi tuong tien an in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching t e m p_ doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn findByUserId_Last(long userId,
        OrderByComparator orderByComparator)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = fetchByUserId_Last(userId,
                orderByComparator);

        if (temp_DoiTuongTienAn != null) {
            return temp_DoiTuongTienAn;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("userId=");
        msg.append(userId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTEMP_DoiTuongTienAnException(msg.toString());
    }

    /**
     * Returns the last t e m p_ doi tuong tien an in the ordered set where userId = &#63;.
     *
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching t e m p_ doi tuong tien an, or <code>null</code> if a matching t e m p_ doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn fetchByUserId_Last(long userId,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByUserId(userId);

        if (count == 0) {
            return null;
        }

        List<TEMP_DoiTuongTienAn> list = findByUserId(userId, count - 1, count,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the t e m p_ doi tuong tien ans before and after the current t e m p_ doi tuong tien an in the ordered set where userId = &#63;.
     *
     * @param doiTuongTienAnId the primary key of the current t e m p_ doi tuong tien an
     * @param userId the user ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next t e m p_ doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn[] findByUserId_PrevAndNext(
        long doiTuongTienAnId, long userId, OrderByComparator orderByComparator)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = findByPrimaryKey(doiTuongTienAnId);

        Session session = null;

        try {
            session = openSession();

            TEMP_DoiTuongTienAn[] array = new TEMP_DoiTuongTienAnImpl[3];

            array[0] = getByUserId_PrevAndNext(session, temp_DoiTuongTienAn,
                    userId, orderByComparator, true);

            array[1] = temp_DoiTuongTienAn;

            array[2] = getByUserId_PrevAndNext(session, temp_DoiTuongTienAn,
                    userId, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected TEMP_DoiTuongTienAn getByUserId_PrevAndNext(Session session,
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn, long userId,
        OrderByComparator orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(6 +
                    (orderByComparator.getOrderByFields().length * 6));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_TEMP_DOITUONGTIENAN_WHERE);

        query.append(_FINDER_COLUMN_USERID_USERID_2);

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(TEMP_DoiTuongTienAnModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(userId);

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(temp_DoiTuongTienAn);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<TEMP_DoiTuongTienAn> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the t e m p_ doi tuong tien ans where userId = &#63; from the database.
     *
     * @param userId the user ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByUserId(long userId) throws SystemException {
        for (TEMP_DoiTuongTienAn temp_DoiTuongTienAn : findByUserId(userId,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(temp_DoiTuongTienAn);
        }
    }

    /**
     * Returns the number of t e m p_ doi tuong tien ans where userId = &#63;.
     *
     * @param userId the user ID
     * @return the number of matching t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByUserId(long userId) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;

        Object[] finderArgs = new Object[] { userId };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_TEMP_DOITUONGTIENAN_WHERE);

            query.append(_FINDER_COLUMN_USERID_USERID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(userId);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the t e m p_ doi tuong tien an in the entity cache if it is enabled.
     *
     * @param temp_DoiTuongTienAn the t e m p_ doi tuong tien an
     */
    @Override
    public void cacheResult(TEMP_DoiTuongTienAn temp_DoiTuongTienAn) {
        EntityCacheUtil.putResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class, temp_DoiTuongTienAn.getPrimaryKey(),
            temp_DoiTuongTienAn);

        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
            new Object[] { temp_DoiTuongTienAn.getSoDinhDanhCaNhan() },
            temp_DoiTuongTienAn);

        temp_DoiTuongTienAn.resetOriginalValues();
    }

    /**
     * Caches the t e m p_ doi tuong tien ans in the entity cache if it is enabled.
     *
     * @param temp_DoiTuongTienAns the t e m p_ doi tuong tien ans
     */
    @Override
    public void cacheResult(List<TEMP_DoiTuongTienAn> temp_DoiTuongTienAns) {
        for (TEMP_DoiTuongTienAn temp_DoiTuongTienAn : temp_DoiTuongTienAns) {
            if (EntityCacheUtil.getResult(
                        TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                        TEMP_DoiTuongTienAnImpl.class,
                        temp_DoiTuongTienAn.getPrimaryKey()) == null) {
                cacheResult(temp_DoiTuongTienAn);
            } else {
                temp_DoiTuongTienAn.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all t e m p_ doi tuong tien ans.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(TEMP_DoiTuongTienAnImpl.class.getName());
        }

        EntityCacheUtil.clearCache(TEMP_DoiTuongTienAnImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the t e m p_ doi tuong tien an.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(TEMP_DoiTuongTienAn temp_DoiTuongTienAn) {
        EntityCacheUtil.removeResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class, temp_DoiTuongTienAn.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache(temp_DoiTuongTienAn);
    }

    @Override
    public void clearCache(List<TEMP_DoiTuongTienAn> temp_DoiTuongTienAns) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (TEMP_DoiTuongTienAn temp_DoiTuongTienAn : temp_DoiTuongTienAns) {
            EntityCacheUtil.removeResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                TEMP_DoiTuongTienAnImpl.class,
                temp_DoiTuongTienAn.getPrimaryKey());

            clearUniqueFindersCache(temp_DoiTuongTienAn);
        }
    }

    protected void cacheUniqueFindersCache(
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn) {
        if (temp_DoiTuongTienAn.isNew()) {
            Object[] args = new Object[] {
                    temp_DoiTuongTienAn.getSoDinhDanhCaNhan()
                };

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                args, Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                args, temp_DoiTuongTienAn);
        } else {
            TEMP_DoiTuongTienAnModelImpl temp_DoiTuongTienAnModelImpl = (TEMP_DoiTuongTienAnModelImpl) temp_DoiTuongTienAn;

            if ((temp_DoiTuongTienAnModelImpl.getColumnBitmask() &
                    FINDER_PATH_FETCH_BY_SODINHDANHCANHAN.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        temp_DoiTuongTienAn.getSoDinhDanhCaNhan()
                    };

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                    args, Long.valueOf(1));
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    args, temp_DoiTuongTienAn);
            }
        }
    }

    protected void clearUniqueFindersCache(
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn) {
        TEMP_DoiTuongTienAnModelImpl temp_DoiTuongTienAnModelImpl = (TEMP_DoiTuongTienAnModelImpl) temp_DoiTuongTienAn;

        Object[] args = new Object[] { temp_DoiTuongTienAn.getSoDinhDanhCaNhan() };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN, args);
        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN, args);

        if ((temp_DoiTuongTienAnModelImpl.getColumnBitmask() &
                FINDER_PATH_FETCH_BY_SODINHDANHCANHAN.getColumnBitmask()) != 0) {
            args = new Object[] {
                    temp_DoiTuongTienAnModelImpl.getOriginalSoDinhDanhCaNhan()
                };

            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                args);
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                args);
        }
    }

    /**
     * Creates a new t e m p_ doi tuong tien an with the primary key. Does not add the t e m p_ doi tuong tien an to the database.
     *
     * @param doiTuongTienAnId the primary key for the new t e m p_ doi tuong tien an
     * @return the new t e m p_ doi tuong tien an
     */
    @Override
    public TEMP_DoiTuongTienAn create(long doiTuongTienAnId) {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = new TEMP_DoiTuongTienAnImpl();

        temp_DoiTuongTienAn.setNew(true);
        temp_DoiTuongTienAn.setPrimaryKey(doiTuongTienAnId);

        return temp_DoiTuongTienAn;
    }

    /**
     * Removes the t e m p_ doi tuong tien an with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param doiTuongTienAnId the primary key of the t e m p_ doi tuong tien an
     * @return the t e m p_ doi tuong tien an that was removed
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn remove(long doiTuongTienAnId)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        return remove((Serializable) doiTuongTienAnId);
    }

    /**
     * Removes the t e m p_ doi tuong tien an with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the t e m p_ doi tuong tien an
     * @return the t e m p_ doi tuong tien an that was removed
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn remove(Serializable primaryKey)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        Session session = null;

        try {
            session = openSession();

            TEMP_DoiTuongTienAn temp_DoiTuongTienAn = (TEMP_DoiTuongTienAn) session.get(TEMP_DoiTuongTienAnImpl.class,
                    primaryKey);

            if (temp_DoiTuongTienAn == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchTEMP_DoiTuongTienAnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(temp_DoiTuongTienAn);
        } catch (NoSuchTEMP_DoiTuongTienAnException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected TEMP_DoiTuongTienAn removeImpl(
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn) throws SystemException {
        temp_DoiTuongTienAn = toUnwrappedModel(temp_DoiTuongTienAn);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(temp_DoiTuongTienAn)) {
                temp_DoiTuongTienAn = (TEMP_DoiTuongTienAn) session.get(TEMP_DoiTuongTienAnImpl.class,
                        temp_DoiTuongTienAn.getPrimaryKeyObj());
            }

            if (temp_DoiTuongTienAn != null) {
                session.delete(temp_DoiTuongTienAn);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (temp_DoiTuongTienAn != null) {
            clearCache(temp_DoiTuongTienAn);
        }

        return temp_DoiTuongTienAn;
    }

    @Override
    public TEMP_DoiTuongTienAn updateImpl(
        qldc.pm.tienan.model.TEMP_DoiTuongTienAn temp_DoiTuongTienAn)
        throws SystemException {
        temp_DoiTuongTienAn = toUnwrappedModel(temp_DoiTuongTienAn);

        boolean isNew = temp_DoiTuongTienAn.isNew();

        TEMP_DoiTuongTienAnModelImpl temp_DoiTuongTienAnModelImpl = (TEMP_DoiTuongTienAnModelImpl) temp_DoiTuongTienAn;

        Session session = null;

        try {
            session = openSession();

            if (temp_DoiTuongTienAn.isNew()) {
                session.save(temp_DoiTuongTienAn);

                temp_DoiTuongTienAn.setNew(false);
            } else {
                session.merge(temp_DoiTuongTienAn);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !TEMP_DoiTuongTienAnModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((temp_DoiTuongTienAnModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        temp_DoiTuongTienAnModelImpl.getOriginalUserId()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
                    args);

                args = new Object[] { temp_DoiTuongTienAnModelImpl.getUserId() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
                    args);
            }
        }

        EntityCacheUtil.putResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            TEMP_DoiTuongTienAnImpl.class, temp_DoiTuongTienAn.getPrimaryKey(),
            temp_DoiTuongTienAn);

        clearUniqueFindersCache(temp_DoiTuongTienAn);
        cacheUniqueFindersCache(temp_DoiTuongTienAn);

        return temp_DoiTuongTienAn;
    }

    protected TEMP_DoiTuongTienAn toUnwrappedModel(
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn) {
        if (temp_DoiTuongTienAn instanceof TEMP_DoiTuongTienAnImpl) {
            return temp_DoiTuongTienAn;
        }

        TEMP_DoiTuongTienAnImpl temp_DoiTuongTienAnImpl = new TEMP_DoiTuongTienAnImpl();

        temp_DoiTuongTienAnImpl.setNew(temp_DoiTuongTienAn.isNew());
        temp_DoiTuongTienAnImpl.setPrimaryKey(temp_DoiTuongTienAn.getPrimaryKey());

        temp_DoiTuongTienAnImpl.setDoiTuongTienAnId(temp_DoiTuongTienAn.getDoiTuongTienAnId());
        temp_DoiTuongTienAnImpl.setUserId(temp_DoiTuongTienAn.getUserId());
        temp_DoiTuongTienAnImpl.setUserName(temp_DoiTuongTienAn.getUserName());
        temp_DoiTuongTienAnImpl.setCreateDate(temp_DoiTuongTienAn.getCreateDate());
        temp_DoiTuongTienAnImpl.setModifiedDate(temp_DoiTuongTienAn.getModifiedDate());
        temp_DoiTuongTienAnImpl.setSoDinhDanhCaNhan(temp_DoiTuongTienAn.getSoDinhDanhCaNhan());
        temp_DoiTuongTienAnImpl.setXoaAnTich(temp_DoiTuongTienAn.getXoaAnTich());
        temp_DoiTuongTienAnImpl.setUndoRedo(temp_DoiTuongTienAn.getUndoRedo());
        temp_DoiTuongTienAnImpl.setThaoTac(temp_DoiTuongTienAn.getThaoTac());
        temp_DoiTuongTienAnImpl.setMainDoiTuongTienAnId(temp_DoiTuongTienAn.getMainDoiTuongTienAnId());

        return temp_DoiTuongTienAnImpl;
    }

    /**
     * Returns the t e m p_ doi tuong tien an with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the t e m p_ doi tuong tien an
     * @return the t e m p_ doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn findByPrimaryKey(Serializable primaryKey)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = fetchByPrimaryKey(primaryKey);

        if (temp_DoiTuongTienAn == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchTEMP_DoiTuongTienAnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return temp_DoiTuongTienAn;
    }

    /**
     * Returns the t e m p_ doi tuong tien an with the primary key or throws a {@link qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException} if it could not be found.
     *
     * @param doiTuongTienAnId the primary key of the t e m p_ doi tuong tien an
     * @return the t e m p_ doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchTEMP_DoiTuongTienAnException if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn findByPrimaryKey(long doiTuongTienAnId)
        throws NoSuchTEMP_DoiTuongTienAnException, SystemException {
        return findByPrimaryKey((Serializable) doiTuongTienAnId);
    }

    /**
     * Returns the t e m p_ doi tuong tien an with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the t e m p_ doi tuong tien an
     * @return the t e m p_ doi tuong tien an, or <code>null</code> if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        TEMP_DoiTuongTienAn temp_DoiTuongTienAn = (TEMP_DoiTuongTienAn) EntityCacheUtil.getResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                TEMP_DoiTuongTienAnImpl.class, primaryKey);

        if (temp_DoiTuongTienAn == _nullTEMP_DoiTuongTienAn) {
            return null;
        }

        if (temp_DoiTuongTienAn == null) {
            Session session = null;

            try {
                session = openSession();

                temp_DoiTuongTienAn = (TEMP_DoiTuongTienAn) session.get(TEMP_DoiTuongTienAnImpl.class,
                        primaryKey);

                if (temp_DoiTuongTienAn != null) {
                    cacheResult(temp_DoiTuongTienAn);
                } else {
                    EntityCacheUtil.putResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                        TEMP_DoiTuongTienAnImpl.class, primaryKey,
                        _nullTEMP_DoiTuongTienAn);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(TEMP_DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                    TEMP_DoiTuongTienAnImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return temp_DoiTuongTienAn;
    }

    /**
     * Returns the t e m p_ doi tuong tien an with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param doiTuongTienAnId the primary key of the t e m p_ doi tuong tien an
     * @return the t e m p_ doi tuong tien an, or <code>null</code> if a t e m p_ doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TEMP_DoiTuongTienAn fetchByPrimaryKey(long doiTuongTienAnId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) doiTuongTienAnId);
    }

    /**
     * Returns all the t e m p_ doi tuong tien ans.
     *
     * @return the t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_DoiTuongTienAn> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the t e m p_ doi tuong tien ans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_DoiTuongTienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of t e m p_ doi tuong tien ans
     * @param end the upper bound of the range of t e m p_ doi tuong tien ans (not inclusive)
     * @return the range of t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_DoiTuongTienAn> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the t e m p_ doi tuong tien ans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.TEMP_DoiTuongTienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of t e m p_ doi tuong tien ans
     * @param end the upper bound of the range of t e m p_ doi tuong tien ans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TEMP_DoiTuongTienAn> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<TEMP_DoiTuongTienAn> list = (List<TEMP_DoiTuongTienAn>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_TEMP_DOITUONGTIENAN);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_TEMP_DOITUONGTIENAN;

                if (pagination) {
                    sql = sql.concat(TEMP_DoiTuongTienAnModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<TEMP_DoiTuongTienAn>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TEMP_DoiTuongTienAn>(list);
                } else {
                    list = (List<TEMP_DoiTuongTienAn>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the t e m p_ doi tuong tien ans from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (TEMP_DoiTuongTienAn temp_DoiTuongTienAn : findAll()) {
            remove(temp_DoiTuongTienAn);
        }
    }

    /**
     * Returns the number of t e m p_ doi tuong tien ans.
     *
     * @return the number of t e m p_ doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_TEMP_DOITUONGTIENAN);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the t e m p_ doi tuong tien an persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.tienan.model.TEMP_DoiTuongTienAn")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<TEMP_DoiTuongTienAn>> listenersList = new ArrayList<ModelListener<TEMP_DoiTuongTienAn>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<TEMP_DoiTuongTienAn>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(TEMP_DoiTuongTienAnImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
